﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace SimplePad
{
    public class FileHandler
    {
        public delegate void dOnExternFileChange(FileHandler file);

        public event dOnExternFileChange OnExternFileChange;

        public delegate void dOnFileChange(FileHandler file);

        public event dOnFileChange OnFileChange;

        public delegate void dOnActivate(FileHandler file);

        public event dOnActivate OnActivate;

        private string _fileName;

        private FileInfo _fileInfo;

        private DockPanel _panel;

        private EditFile _window;

        private FileSystemWatcher _watcher;

        private bool _changed = false;

        public bool IsReadOnly { get; private set; }

        public FileHandler(string filename, DockPanel panel)
        {
            _fileName = filename;
            _fileInfo = new FileInfo(filename);
            _panel = panel;

            IsReadOnly = _fileInfo.IsReadOnly;
            
            _watcher = new FileSystemWatcher(_fileInfo.Directory.FullName, _fileInfo.Name);
            _watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes;
            _watcher.Changed += _watcher_Changed;
            _watcher.IncludeSubdirectories = false;
            _watcher.EnableRaisingEvents = true;

            _window = new EditFile(this);
            _window.Show(panel);
        }

        public void Activate()
        {
            if (OnActivate != null) OnActivate(this);
        }

        void _watcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Changed && !_changed)
            {
                _changed = true;
                if (OnExternFileChange != null) OnExternFileChange(this);
                if (OnFileChange != null) OnFileChange(this);
                //MessageBox.Show("File Changed");
            }
        }

        public void Save(string content, string fileName)
        {
            StreamWriter sw = new StreamWriter(fileName);
            sw.Write(content);
            sw.Close();
            if (!_window.IsClosing)
            {
                _changed = false;
                if (OnFileChange != null)
                    OnFileChange(this);
            }
        }

        public void Save(string content)
        {
            Save(content,_fileInfo.FullName);
        }

        public void Close()
        {
            _window.Close();
        }

        public string FileName
        {
            get { return _fileInfo.Name; }
        }

        public string FullName
        {
            get { return _fileInfo.FullName; }
        }

        public DockPanel DockPanel
        {
            get { return _panel; }
        }

        public bool Changed
        {
            get { return _changed; }
            set
            {
                _changed = value;
                if (OnFileChange != null)
                    OnFileChange(this);
            }
        }

        public Icon getFileIcon()
        {
            IntPtr icon = IntPtr.Zero;
            if (_changed)
            {
                icon = Properties.Resources.bullet_error.GetHicon();
            }
            else if (_fileInfo.IsReadOnly)
            {
                icon = Properties.Resources.bullet_black.GetHicon();
            }
            else
            {
                icon = Properties.Resources.bullet_blue.GetHicon();
            }
            return (icon == IntPtr.Zero) ? null : Icon.FromHandle(icon);
        }
    }
}
